package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.TRukuDao;
import com.aaa.javaweb.entity.TRuku;
import com.aaa.javaweb.until.JDBCUntil;
import com.aaa.javaweb.until.TransBean;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class TRukuDaoImpl implements TRukuDao {

    @Override
    public int addRuku(Map map) {
        String sql = "insert into t_ruku (rukuid,rdate,uid,totalprice,totalcount,jsr) values (null,?,?,?,?,?)";
        if (map != null) {
            return JDBCUntil.Updatekeys(sql, new Date(), map.get("uid"),map.get("totalprice"), map.get("totalcount"), map.get("jsr"));
        }
        return -1;
    }

    @Override
    public int addRukuDetail(List<Map> tableData, int rukuid) {
        String sql = "insert into t_rukudetail (rdid,ssid,count,rukuid,price) values (null,?,?,?,?)";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql, map.get("ssid"), map.get("count"), rukuid, map.get("price"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public int rukuAndAddBookCount(List<Map> tableData) {
        String sql = "update t_sheshi set count = count + ? where id = ? ";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql, map.get("count"), map.get("ssid"));
            }
            return i;
        }
        return 0;
    }

    @Override
    public long getCount(TRuku ruku) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_ruku r left join t_staff s on r.uid = s.id where 1=1");
        if (ruku.getSname()!=null&&ruku.getSname()!=""){
            sql.append(" and s.sname like '%").append(ruku.getSname()).append("%'");
        }
        if (ruku.getJsr()!=null&&ruku.getJsr()!=""){
            sql.append(" and r.jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getCreatedTime()!=null&&ruku.getCreatedTime()!=""){
            sql.append(" and r.rdate >= '").append(ruku.getCreatedTime()).append("'");
        }
        if (ruku.getEndTime()!=null&&ruku.getEndTime()!=""){
            sql.append(" and r.rdate <= '").append(ruku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc ");
        List<Map> maps = JDBCUntil.selectMap(sql + "");
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0L;
    }

    @Override
    public List<TRuku> getAllRuku(TRuku ruku) {
        StringBuilder sql = new StringBuilder("select r.rukuid,r.rdate,r.uid,r.totalprice,r.totalcount,r.jsr,s.sname from t_ruku r left join t_staff s on r.uid = s.id where 1=1");
        if (ruku.getSname()!=null&&ruku.getSname()!=""){
            sql.append(" and s.sname like '%").append(ruku.getSname()).append("%'");
        }
        if (ruku.getJsr()!=null&&ruku.getJsr()!=""){
            sql.append(" and r.jsr like '%").append(ruku.getJsr()).append("%'");
        }
        if (ruku.getCreatedTime()!=null&&ruku.getCreatedTime()!=""){
            sql.append(" and r.rdate >= '").append(ruku.getCreatedTime()).append("'");
        }
        if (ruku.getEndTime()!=null&&ruku.getEndTime()!=""){
            sql.append(" and r.rdate <= '").append(ruku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc limit ?,?");
        List<Map> maps = JDBCUntil.selectMap(sql + "", (ruku.getPage() - 1) * ruku.getLimit(), ruku.getLimit());
        return TransBean.populate(TRuku.class,maps);
    }

    @Override
    public List<Map> getAllRukuDetailByRukuId(String rukuid) {
        String sql = "select r.rdid,r.rukuid,r.ssid,r.count,r.price,b.name,b.gys from t_rukudetail r join t_sheshi b on r.ssid = b.id where r.rukuid = ?";
        return JDBCUntil.selectMap(sql, rukuid);
    }
}
